package net.jblog.struts.backoffice.action;

import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jblog.business.ServiceLocatorReference;
import net.jblog.business.facade.AdministratorUserFacade;
import net.jblog.data.model.AdministratorUser;
import net.jblog.struts.backoffice.form.AdministratorUserForm;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


public class LoginAction extends Action {
	@SuppressWarnings("unused")
	private Logger log = Logger.getLogger(LoginAction.class.getName());
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
		HttpServletRequest request, HttpServletResponse response)
		throws Exception {
		
		AdministratorUserFacade facade = ServiceLocatorReference.getServiceLocator().getAdministratorUSerFacade();
		AdministratorUserForm userForm = (AdministratorUserForm) form;
		
		AdministratorUser administratorUser = facade.findByEmailPassword(userForm.getEmail(), userForm.getPassword());
		if(administratorUser != null) {
			request.getSession().setAttribute("ADMINISTRATOR", administratorUser);
			return mapping.findForward("success");
		}

		return mapping.findForward("error");
		
		
		
        /*String content = "content";
        Date date = new Date();
        Greeting greeting = new Greeting("test", content, date);

        PersistenceManager pm = DataManagerFactory.get().getPersistenceManager();
        try {
            pm.makePersistent(greeting);
        } finally {
            pm.close();
        }*/
		
	}


}
